#! /usr/bin/env python

import os
import sys
import shutil
import getopt

def make_symlinks(path,isVim=False,isEmacs=False,isModmap=False,isAwesome=False):
	home = os.getenv("HOME")
	if home:
		os.chdir(home)
	else:
		os.system("cd ~") #default use shell command
	if isVim :
		if os.path.exists(".vim"):
			if os.path.islink(".vim"):
				os.remove(".vim")
			else:
				shutil.move(".vim",".vim.cp")
		os.symlink(path + "/vim",".vim")

		if os.path.exists(".vimrc"):
			if os.path.islink(".vimrc"):
				os.remove(".vimrc")
			else:
				shutil.move(".vimrc",".vimrc.cp")
		os.symlink(path + "/vim/vimrc",".vimrc")

	if isEmacs : 
		if os.path.exists(".emacs"):
			if os.path.islink(".emacs"):
				os.remove(".emacs")
			else:
				shutil.move(".emacs",".emacs.cp")
		os.symlink(path + "/emacs/emacs",".emacs")

	if isModmap:
		if os.path.exists(".Xmodmap"):
			if os.path.islink(".Xmodmap"):
				os.remove(".Xmodmap")
			else:
				shutil.move(".Xmodmap",".Xmodmap.cp")
		os.symlink(path + "/sys/Xmodmap",".Xmodmap")

	if isAwesome : 
		os.chdir("/etc/xdg/awesome/")
		if os.path.exists("rc.lua"):
			if os.path.islink("rc.lua"):
				os.remove("rc.lua")
			else:
				shutil.move("rc.lua","rc.lua.cp")
		os.symlink(path + "/sys/rc.lua","rc.lua")

		os.chdir("/usr/share/awesome/themes/default/")
		if os.path.exists("theme.lua"):
			if os.path.islink("theme.lua"):
				os.remove("theme.lua")
			else:
				shutil.move("theme.lua","theme.lua.cp")
		os.symlink(path + "/sys/theme.lua","theme.lua")

if __name__ == "__main__":
	path = ""
	isVim = False
	isEmacs = False
	isModmap = False
	isAwesome = False
	opts,args = getopt.getopt(sys.argv[1:],"hpvema:",["help","path=","vim","emacs","modmap","awesome"])
	for o,a in opts:
		if o in ("-h","--help"):
			print "-h,--help: get the progame help"
			print "-p,--path: the system settings path,default is \"/opt/sys/settings/bin\""
			print "-v,--vim: create the vim symlink"
			print "-e,--emacs:create the emacs symlink"
			print "-m,--modmap:create the Xmodmap symlink"
			print "-a,--awesome:create the awesome config symlink"
			sys.exit()
		elif o in ("-p","--path"):
			path = a
		elif o in ("-v","--vim"):
			isVim = True
		elif o in ("-e","--emacs"):
			isEmacs = True
		elif o in ("-m","--modmap"):
			isModmap = True
		elif o in ("-a","--awesome"):
			isAwesome = True
	if path == "":
		path = "/opt/sys/settings/bin"
	make_symlinks(path,isVim,isEmacs,isModmap,isAwesome)
